package cn.myblog.ainblog.conroller.backstage;

import cn.myblog.ainblog.dto.ReplyDTO;
import cn.myblog.ainblog.model.CommentModel;
import cn.myblog.ainblog.model.ReplyModel;
import cn.myblog.ainblog.service.ArticleService;
import cn.myblog.ainblog.service.CommentService;
import cn.myblog.ainblog.service.ReplyService;
import cn.myblog.ainblog.vo.CommentAndArticleVO;
import cn.myblog.ainblog.vo.PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.awt.event.MouseMotionAdapter;
import java.util.Iterator;
import java.util.List;

/**
 * @author Dovaior
 * @Description 后台留言管理的控制器
 * @create 2021-05-07 15:00
 **/
@Controller
@RequestMapping("/backstage")
public class BackstageCommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ReplyService replyService;

    /**
     * 留言管理界面，分页展示存在留言的博文及留言信息
     * @param current 指定当前页
     * @param model 存放封装的信息
     */
    @GetMapping("/commentManage/{current}")
    public String commentManage(@PathVariable Integer current, Model model){
        //每页10条
        Integer size = 10;
        //封装page信息
        IPage<CommentAndArticleVO> page = new Page<>(current,size);
        //进行查询
        commentService.selectCommentListPage(page);
        PageVO<CommentAndArticleVO> commentArticleVO = new PageVO<>();
        //为pageVO赋值
        commentArticleVO.setSize(size);
        commentArticleVO.setCurrent(current);
        //将总条目数转换为总页数
        Long pageSize = page.getTotal() % size;
        if (pageSize == 0){
            commentArticleVO.setTotal(page.getTotal()/size);
        }else{
            commentArticleVO.setTotal((page.getTotal()/size) + 1);
        }
        //封装查询出的信息
        commentArticleVO.setPageList(page.getRecords());
        model.addAttribute("commentArticleVO",commentArticleVO);
        return "backstage/backstage-commentManage";
    }

    /**
     * 根据指定博文id删除其全部留言
     * @param articleId 指定博文id
     * @return 返回标识
     */
    @ResponseBody
    @GetMapping("/deleteAllCommentByArticleId/{articleId}")
    public boolean deleteAllCommentByArticleId(@PathVariable String articleId){
        //根据博文查询留言id
        List<Long> commentIdsList = commentService.queryCommentIdsByArticleId(articleId);
        //根据留言ID批量删除回复
        for (Long commentId:commentIdsList){
            replyService.clearReplysByCommentId(commentId);
        }
        //删除操作
        int i = commentService.deleteAllCommentByArticleId(articleId);
        //该博文的留言数量清零
        articleService.clearCommentCountByArticleId(articleId);
        if (i == 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 分页展示具体某博文的全部留言信息
     * @param articleId 指定博文
     * @param current 当前页
     * @param model 存放封装信息
     */
    @GetMapping("/commentList/{current}/{articleId}")
    public String commentList(@PathVariable String articleId,@PathVariable Integer current,Model model){
        model.addAttribute("articleTitle",articleService.queryArticleTitleById(articleId));
        Integer size = 9;
        PageVO<CommentModel> commentVO = new PageVO<>();
        IPage<CommentModel> page = new Page<>(current,size);
        commentService.selectCommentPage(page,articleId);
        commentVO.setCurrent(current);
        commentVO.setSize(size);
        Long pageSize = page.getTotal() % size;
        if (pageSize == 0){
            commentVO.setTotal(page.getTotal()/size);
        }else{
            //不能整除页码加一
            commentVO.setTotal((page.getTotal()/size)+1);
        }
        commentVO.setPageList(page.getRecords());
        model.addAttribute("commentListById",commentVO);
        return "backstage/backstage-commentList";
    }

    /**
     * 删除某博文下的一条留言
     * @param current 当前页码current
     * @param articleId 指定博文id
     * @param commentId 指定的留言id
     */
    @GetMapping("/deleteCommentByCommentId/{current}/{articleId}/{commentId}")
    public String deleteCommentByCommentId(@PathVariable Integer current,@PathVariable String articleId,@PathVariable Long commentId){
        //判断回复状态
        int replyStatus = commentService.isReplyStatus(commentId);
        //若存在回复
        if (replyStatus == 1){
            //删除当前留言下的回复
            replyService.clearReplysByCommentId(commentId);
        }
        //删除操作
        commentService.deleteOneCommentByCommentId(commentId);
        //留言数减一
        articleService.deFreshCommentCountByArticleId(articleId);
        //重定向
        return "redirect:/backstage/commentList/"+current+"/"+articleId;
    }

    /**
     * 查询指定留言的内容
     * @param commentId 指定的留言id
     */
    @ResponseBody
    @GetMapping("/queryCommentContentByCommentId/{commentId}")
    public String queryCommentContentByCommentId(@PathVariable Long commentId){
        return commentService.queryCommentContentByCommentId(commentId);
    }

    /**
     * 查询指定留言的状态
     * @param commentId 指定的留言id
     */
    @ResponseBody
    @GetMapping("/isReplyStatus/{commentId}")
    public int isReplyStatus(@PathVariable Long commentId){
        return commentService.isReplyStatus(commentId);
    }

    /**
     * 回复留言操作
     * @param replyDTO 封装回复信息的实体
     */
    @ResponseBody
    @PostMapping("/replyMessage")
    public boolean replyMessage(ReplyDTO replyDTO){
        ReplyModel replyModel = new ReplyModel();
        BeanUtils.copyProperties(replyDTO,replyModel);
        int i = replyService.addReply(replyModel);
        if (i != 0){
            //修改留言中的回复状态
            commentService.changeStatus(replyModel.getCommentId());
            //回复成功
            return true;
        }else {
            //回复失败
            return false;
        }
    }

    /**
     * 查询指定留言的回复
     * @param commentId 指定的留言的id
     * @return 返回查询出来的内容
     */
    @ResponseBody
    @GetMapping("/queryReplyContentByCommentId/{commentId}")
    public String queryReplyContentByCommentId(@PathVariable Long commentId){
        return replyService.queryReplyContentByCommentId(commentId);
    }

    /**
     * 删除指定的留言的回复
     * @param commentId 指定的留言id
     */
    @ResponseBody
    @GetMapping("/deleteReplyByCommentId/{commentId}")
    public boolean deleteReplyByCommentId(@PathVariable Long commentId){
        //将留言的回复状态该为“未回复”
        commentService.deChangeStatus(commentId);
        //删除该条回复
        int i = replyService.clearReplysByCommentId(commentId);
        if (i == 0){
            //删除失败
            return false;
        }else{
            //删除成功
            return true;
        }
    }

    /**
     * 修改指定留言的回复
     * @param commentId 指定留言的id
     * @param replyContent 修改后的回复内容
     */
    @ResponseBody
    @GetMapping("/submitReplyChanges/{commentId}/{replyContent}")
    public boolean submitReplyChanges(@PathVariable Long commentId,@PathVariable String replyContent){
        int flag = replyService.submitReplyChanges(commentId, replyContent);
        if (flag == 0){
            return false;
        }else{
            return true;
        }
    }

}
